﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;
using Hangjing.WebCommon;

namespace Html5
{
    public partial class shopdetail : System.Web.UI.Page
    {
        ETogoOpinion dal = new ETogoOpinion();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Points daltogo = new Points();
                int id = HjNetHelper.GetQueryInt("id", 0);
                tid.Value = id.ToString();
                togourl.HRef = "ShowTogo.aspx?id=" + id;
                try
                {
                    int count = dal.GetCount("TogoID=" + id);
                    IList<ETogoOpinionInfo> infolist = dal.GetList(1000, 1, "TogoID=" + id, "PostTime", 1);
                    int ServiceGrade = 0;
                    int FlavorGrade = 0;
                    int SpeedGrade = 0;
                    int all = 0;
                    foreach (var item in infolist)
                    {
                        ServiceGrade += item.ServiceGrade;
                        FlavorGrade += item.FlavorGrade;
                        SpeedGrade += item.SpeedGrade;
                        all += 15;
                    }

                    if (count > 0)
                    {
                        int rev = ((ServiceGrade + FlavorGrade + SpeedGrade) * 100 / all);
                        review.InnerHtml = rev + "%";
                        sugcut.InnerHtml = count.ToString();
                    }
                }
                catch (Exception ex)
                {
                    HJlog.toLog(ex.ToString());
                }

                string SqlWhere = String.Format("Unid={0}", id);

                string strDistance = " 1=1 ";
                string lat = WebUtility.FixgetCookie("mylat");
                string lng = WebUtility.FixgetCookie("mylng");

                if (lat == "" || lng == "" || lat == null || lng == null)
                {
                    lat = "0";
                    lng = "0";
                    Response.Redirect("index.aspx");
                    return;
                }
            

                IList<PointsInfo> shoplist = daltogo.GetDistanceListSuper(1, 1, SqlWhere, "unid", 1, lat, lng, strDistance);

                //IList<wechatshopdeliveryInfo> deliveryrecord = new wechatshopdelivery().GetList(" tid = " + id);

                IList<ShopFoodPictureInfo> alltags = CacheHelper.GetShopPicTag();
                foreach (var item in shoplist)
                {
                    //foreach (var record in deliveryrecord)
                    //{
                    //    if (record.distancestart <= item.Distance && record.distanceend > item.Distance)
                    //    {

                    //        item.SendFee = record.sendmoney;
                    //        item.SendLimit = record.minmoney;
                    //        break;
                    //    }
                    //}

                    SendInfo sendata = new wechatOrderFeeConfig().countSendMoney(item, item.Distance);
                    item.SendFee = sendata.sendmoney;


                    item.pictags = new List<ShopFoodPictureInfo>();
                    string[] tagids = WebUtility.delBrackets(item.OpenTime).Split(',');
                    foreach (var ids in tagids)
                    {
                        foreach (var tag in alltags)
                        {
                            if (ids.Trim() == tag.IID.ToString().Trim())
                            {
                                item.pictags.Add(tag);
                            }
                        }
                    }

                    


                }
                IList<webPromotionConfigInfo> shoppromotions = Hangjing.WebCommon.WebHelper.getShopPromotions(shoplist[0].Unid, shoplist[0].PType, shoplist[0].PEnd);
                foreach (var item in shoppromotions)
                {
                    ShopFoodPictureInfo tag = new ShopFoodPictureInfo();
                    tag.Title = item.revevar1;
                    tag.Picture = "/images/jian_02.png";
                    shoplist[0].pictags.Add(tag);
                }


                this.rpttags.DataSource = shoplist[0].pictags;
                this.rpttags.DataBind();

                PointsInfo togomodel = shoplist[0];

              

                int iscollect = 0;//是否收藏，0表示没有，1表示已经收藏
                ECustomerInfo user = UserHelp.GetUser();
                if (user != null)
                {
                    hidUid.Value = user.DataID.ToString();
                    ETogoCollect dal_c = new ETogoCollect();
                    string sql = " 1= 1 and inve1=0 and togoid=" + id + " and userid=" + user.DataID;
                    if (dal_c.GetTogoCount(sql) > 0)
                    {
                        iscollect = 1;
                        collect.Value = "1";
                    }
                }
                //餐车的起送价
                //shopimg.Style["background-image"] = "url(" + WebUtility.ShowPic(togomodel.Picture) + ")";
                shopimgs.Src = WebUtility.ShowPic(togomodel.Picture);
                hfminimoney.InnerHtml = togomodel.SendLimit.ToString();

                Ashoptel.HRef = "tel:"+togomodel.Comm;
                opentime1.InnerHtml = togomodel.opentimestr;
                sendfree.InnerHtml = togomodel.SendFee.ToString();//配送费
                h1togoname.InnerHtml = togomodel.Name;
                distance.InnerHtml = togomodel.Distance.ToString("0.0");
                this.shopName.InnerHtml = togomodel.Name;
                this.address.InnerHtml = togomodel.Address;//后期添加 2015-12-2 
            }

        }
    }
}